package org.cypress.rtpack.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.cypress.rtpack.common.core.Result;
import org.cypress.rtpack.common.exception.BizException;
import org.cypress.rtpack.common.exception.ExceptionCode;
import org.cypress.rtpack.entity.RtpackQushi;

import org.cypress.rtpack.entity.RtpackQutag;
import org.cypress.rtpack.mapper.RtpackQushiMapper;
import org.cypress.rtpack.service.IRtpackQushiService;
import org.cypress.rtpack.service.IRtpackQutagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * @author getty
 */
@Service
public class RtpackQushiServiceImpl extends ServiceImpl<RtpackQushiMapper, RtpackQushi> implements IRtpackQushiService {
    @Autowired
    RtpackQutagServiceImpl rtpackQutagService;
    @Override
    public Result addQushi(RtpackQushi rtpackQushi) {
        if (Objects.isNull(rtpackQushi)) {
            throw new BizException(ExceptionCode.fail);
        }
        // 生成UUID
        String uuid = UUID.randomUUID().toString();
        for (RtpackQutag rtpackQutag: rtpackQushi.getPoints()) {
            rtpackQutag.setTh(uuid);
            rtpackQutagService.addQutag(rtpackQutag);
        }
        rtpackQushi.setId(uuid);
        boolean success = save(rtpackQushi);
        return success ? Result.ok() : Result.fail("添加趋势图失败");
    }

    @Override
    public Result deleteQushi(String id) {
        if (Objects.isNull(id)) {
            throw new BizException(ExceptionCode.requestMethodError);
        }
        rtpackQutagService.deleteQutagByth(id);
        boolean success = removeById(id);
        return success ? Result.ok() : Result.fail("删除趋势图失败");
    }

    @Override
    public Result updateQushi(RtpackQushi rtpackQushi) {
        if (Objects.isNull(rtpackQushi) || Objects.isNull(rtpackQushi.getId())) {
            throw new BizException(ExceptionCode.requestMethodError);
        }
        boolean success = updateById(rtpackQushi);
        return success ? Result.ok() : Result.fail("更新趋势图失败");
    }

    @Override
    public Result getQushiList() {
        List<RtpackQushi> list = list();
        for (RtpackQushi rtpackQushi:list){
            List<RtpackQutag> rtpackQutag = rtpackQutagService.getQutagByth(rtpackQushi.getId());
            rtpackQushi.setPoints(rtpackQutag);
        }
        return Result.ok(list);
    }

    @Override
    public Result getQushiById(String id) {
        if (Objects.isNull(id)) {
            throw new BizException(ExceptionCode.requestMethodError);
        }
        RtpackQushi rtpackQushi = getById(id);
        return Result.ok(rtpackQushi);
    }
}
